<template>
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        @confirm="handleSubmit"
        @close="handleClose"
    >
        <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
            <el-form-item label="岗位名称" prop="name">
                <el-input
                    v-model="formData.name"
                    placeholder="请输入岗位名称"
                    clearable
                    :maxlength="100"
                />
            </el-form-item>
    
    
            <el-form-item label="岗位编码" prop="code">
                <el-input v-model="formData.code" placeholder="请输入岗位编码" clearable />
            </el-form-item>
    
    
            <el-form-item label="排序" prop="sort">
                <div>
                    <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                    <div class="form-tips">默认为0， 数值越大越排前</div>
                </div>
            </el-form-item>
    
    
            <el-form-item label="备注" prop="remarks">
                <el-input
                    v-model="formData.remarks"
                    placeholder="请输入备注"
                    type="textarea"
                    :autosize="{ minRows: 4, maxRows: 6 }"
                    maxlength="200"
                    show-word-limit
                />
            </el-form-item>
    
            <el-form-item label="岗位状态" prop="isStop">
                <el-switch v-model="formData.isStop" :active-value="0" :inactive-value="1" />
            </el-form-item>
        </el-form>
    
    </popup>
</template>

<script lang='ts' setup>

import {FormInstance} from "element-plus";
import Popup from "@/components/popup/index.vue"
import feedback from "@/utils/feedback";
import {getPostDetailById, postAdd, postEdit} from "@/api/system/SystemAuthPostController";


const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()

const mode = ref('add')

/**
 * 计算标题
 */
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑岗位' : '新增岗位'
})

/**
 * 数据
 */
const formData = reactive({
    id: '',
    name: '',
    code: '',
    sort: 0,
    remarks: '',
    isStop: 0
})


/**
 * 提交
 */
const emit = defineEmits(['success', 'close'])

async function handleSubmit() {
    await formRef.value?.validate()
    mode.value == 'edit' ? await postEdit(formData) : await postAdd(formData)
    feedback.msgSuccess('操作成功')
    popupRef.value?.close()
    emit('success')
}

/**
 * 取消
 */
function handleClose() {
    emit('close')
}


/**
 * 主页面编辑处理
 *
 * @param row
 */
async function setFormData(row: Record<string, any>) {
    await getPostDetailById({id: row.id}).then(info => {
        for (const key in formData) {
            if (!!info[key]) {
                //@ts-ignore
                formData[key] = info[key]
            }
        }
    })
}


/**
 * 供主页面打开
 *
 * @param type
 */
function open(type = 'add') {
    mode.value = type
    popupRef.value?.open()
}

const formRules = {
    code: [{
            required: true,
            message: '请输入岗位编码',
            trigger: ['blur']}
    ],
    name: [
        {
            required: true,
            message: '请输入岗位名称',
            trigger: ['blur']
        }
    ]
}


defineExpose({
    open,
    setFormData
})

</script>
